package cn.zwx.member.svc.manager.impl;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.enums.ResponseCodeEnum;
import cn.zwx.base.exception.BizWrapException;
import cn.zwx.distributed.svc.rpc.facade.DubboDistributedService;
import cn.zwx.member.svc.manager.DistributedManager;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @author zhangwenxue
 */
@Component
public class DistributedManagerImpl implements DistributedManager {


    @DubboReference(version = "v1.0.0")
    private DubboDistributedService dubboDistributedService;


    @Override
    public String getDistributedId() throws BizWrapException {
        try {
            ResponseEntity<String> responseEntity = dubboDistributedService.getDistributedId();
            if (!responseEntity.isStatus()){
                throw new BizWrapException(ResponseCodeEnum.RPC_ERROR.getCode(),responseEntity.getData());
            }
            return responseEntity.getData();
        } catch (Exception e){
            if (e instanceof BizWrapException){
                throw e;
            }
            throw new BizWrapException(ResponseCodeEnum.RPC_ERROR);
        }
    }
}
